﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows;
using DBDataAccessLib.DataMapping;

namespace PluginManager
{
    [Flags]
    public enum Permission
    {
        None = 0,
        UploadImages = 1,
        ManageImages = 2,
        UploadInfo = 4,
        ManageInfo = 8,
        PlayInfo = 16,
        ManageUser = 32,
        ManageSysParam = 64,
    }

    public class PluginObj : NamePair
    {
        public object Instance { get; set; }
    }

    public class NamePair
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public int PermissionID { get; set; }
    }
    public class PluginsManagement
    {
        public static PluginsManagement Instance = new PluginsManagement();
        private string baseDir;
        private List<PluginObj> registedInstances = new List<PluginObj>();
        private PluginsManagement()
        {
            RegistInstancesBasedOnPermission();
        }

        public void RegistInstancesBasedOnPermission()
        {
            registedInstances.Clear();
            baseDir = AppDomain.CurrentDomain.BaseDirectory;
            int permission = int.Parse((GeneralInformation.ViewStates["UserInfo"] as AccountMapping).Function);
            if (!Directory.Exists(baseDir + "PluginModules")) return;
            string[] filePaths = Directory.GetFiles(baseDir + "PluginModules");
            foreach (var path in filePaths)
            {
                Assembly ass = Assembly.LoadFile(path);
                Type[] types = ass.GetTypes();
                foreach (Type type in types)
                {
                    if (type.GetInterface("IPluginNames") != null && type.GetInterface("IPluginView") != null)
                    {
                        IPluginNames nameContainer = (IPluginNames)Activator.CreateInstance(type);
                        if ((permission & nameContainer.PermissionID) != 0)
                        {
                            registedInstances.Add(
                                new PluginObj
                                {
                                    ID = nameContainer.PluginID,
                                    Name = nameContainer.PluginName,
                                    Instance = nameContainer,
                                    PermissionID = nameContainer.PermissionID,
                                });
                        }
                    }
                }
            }
            registedInstances = registedInstances.OrderBy(p => p.PermissionID).ToList();
        }

        public List<NamePair> GetAllPluginsNamePair()
        {
            List<NamePair> names = new List<NamePair>();
            registedInstances.ForEach(p => names.Add(new NamePair { Name = p.Name, ID = p.ID }));
            return names;
        }

        public DependencyObject GetView(string id)
        {
            PluginObj obj = registedInstances.FirstOrDefault(p => p.ID == id);
            if (obj == null) return null;
            else return (obj.Instance as IPluginView).View;
        }
    }
}
